import {Component, OnInit} from '@angular/core';
import {AisiPriceService} from '../aisi-price.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-back-manage',
  templateUrl: './back-manage.component.html',
  styleUrls: ['./back-manage.component.scss']
})
export class BackManageComponent implements OnInit {
  condition = {
    version: 'china', // china国行 foreign外版
    priceType: 'recycle', // recycle回收价 market销售价
  };
  levelAbout = [
    {level: '充新', remark: '(靓机高标准)在靓机的基础上，屏壳无任何划痕，使用痕迹，电池效率高，保修天数高'},
    {level: '靓机', remark: '屏(肉眼看无明显划痕)壳(表面轻微使用痕迹，无明显划痕)整体表现95新以上'},
    {level: '小花', remark: '屏(肉眼可见细微划痕，不能硬伤，贴膜可以覆盖)壳(中框可见轻微磕碰)整体成色9成新以上'},
    {level: '中花', remark: '屏(肉眼可见划痕，不能爆)壳(中框明显磕碰，不能变形)整体成色85新左右'},
    {level: '大花/爆屏', remark: '屏(多处划痕，大硬伤，玻璃爆，内屏显示正常)壳(中框多处磕碰，变形，不能断裂，主板不能弯曲)整体成色8成新以下'},
  ];
  table: any[] = [];
  loading = false;

  constructor(
    private aisiPriceService: AisiPriceService,
    private msg: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.queryPrice();
  }

  // 0-苹果国行（回收报价）、1-苹果外版（回收报价）、2-苹果国行（商家采购报价）、3-苹果外版（商家采购报价）
  queryPrice() {
    this.loading = true;
    let type: any = this.condition.version + this.condition.priceType;
    switch (type) {
      case 'chinarecycle':
        type = 0;
        break;
      case 'foreignrecycle':
        type = 1;
        break;
      case 'chinamarket':
        type = 2;
        break;
      case 'foreignmarket':
        type = 3;
        break;
    }
    this.aisiPriceService.httpGet(this.aisiPriceService.serviceUrl.quote, {type}).then((res: any) => {
      this.loading = false;
      this.table = [];
      if (res.code === 0 && res.data) {
        const records = JSON.parse(res.data?.content);
        this.formatData(records);
      }
    }).catch(err => {
      this.loading = false;
      console.log(err);
      this.msg.error(err.message);
    });
  }

  // 将获取到的数据格式化为表格需要的数据格式
  formatData(list: any[]) {
    list.forEach(table => {
      const tableResult = {title: table.title, list: []};
      table.list.forEach((type: any) => {
        const result: any[] = [];
        type.list.reverse().forEach((item: any) => {
          if (Reflect.has(item, '规格')) {
            result.push({specification: item['规格']});
          } else if (Reflect.has(item, '充新')) {
            result[result.length - 1].chongxin = {aisi: item['充新']['爱思'], shoushou: item['充新']['收收'].split(/\(|\)/)};
          } else if (Reflect.has(item, '靓机')) {
            result[result.length - 1].liangji = {aisi: item['靓机']['爱思'], shoushou: item['靓机']['收收'].split(/\(|\)/)};
          } else if (Reflect.has(item, '小花')) {
            result[result.length - 1].xiaohua = {aisi: item['小花']['爱思'], shoushou: item['小花']['收收'].split(/\(|\)/)};
          } else if (Reflect.has(item, '中花')) {
            result[result.length - 1].zhonghua = {aisi: item['中花']['爱思'], shoushou: item['中花']['收收'].split(/\(|\)/)};
          } else if (Reflect.has(item, '大花')) {
            result[result.length - 1].dahua = {aisi: item['大花']['爱思'], shoushou: item['大花']['收收'].split(/\(|\)/)};
          } else if (Reflect.has(item, '爆屏')) {
            result[result.length - 1].baoping = {aisi: item['爆屏']['爱思'], shoushou: item['爆屏']['收收'].split(/\(|\)/)};
          }
        });
        type.list = result.reverse();
        console.log(type);
        type.list.forEach((specification: any, index: number) => {
          if (index === 0) {
            // @ts-ignore
            tableResult.list.push({minTitle: type.minTitle, length: type.list.length, remark: type.remark, ...specification});
          } else {
            // @ts-ignore
            tableResult.list.push({minTitle: type.minTitle, length: 0, remark: type.remark, ...specification});
          }
        });
      });
      this.table.push(tableResult);
    });
  }
}
